//
//  UIViewController+Kit.m
//  demo
//
//  Created by iMac on 2019/6/3.
//  Copyright © 2019年 iMac. All rights reserved.
//

#import "UIViewController+Kit.h"

@implementation UIViewController (Kit)

+ (UIViewController *)currentTopViewController
{
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([rootVc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarVC = (UITabBarController *)rootVc;
        if ([tabBarVC.selectedViewController isKindOfClass:[UINavigationController class]]) {
            UINavigationController *naviVC = (UINavigationController *)tabBarVC.selectedViewController;
            return naviVC.topViewController;
        }
        else {
            return tabBarVC.selectedViewController;
        }
    }
    else if ([rootVc isKindOfClass:[UINavigationController class]]) {
        UINavigationController *naviVC = (UINavigationController *)rootVc;
        return naviVC.topViewController;
    }
    else {
        return rootVc;
    }
}

+ (UIViewController *)tabbarSelectedViewController
{
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([rootVc isKindOfClass:[UITabBarController class]]) {
        UITabBarController *tabBarVC = (UITabBarController *)rootVc;
        return tabBarVC.selectedViewController;
    }
    else {
        return nil;
    }
}

+ (UITabBarController *)currentTabBarController
{
    UIViewController *rootVc = [UIApplication sharedApplication].keyWindow.rootViewController;
    if ([rootVc isKindOfClass:[UITabBarController class]]) {
        return (UITabBarController *)rootVc;
    }
    else {
        return nil;
    }
}

@end
